import Vue from 'vue';
import Vuex from 'vuex';


Vue.use(Vuex);

const types = {
    SET_IS_AUTHENTICATED: 'SET_IS_AUTHENTICATED', // 是否认证通过
    SET_USER: 'SET_USER', // 用户信息
    SET_IS_TOLOGIN: 'SET_IS_TOLOGIN' //展示登录注册弹窗
}

const store = new Vuex.Store({
    state: {
        isAuthenticated: false, // 是否认证
        user: {}, // 存储用户信息
        isToLogin: null

    },

    getters: {
        isAuthenticated: state => state.isAuthenticated,
        user: state => state.user,
        isToLogin: state => state.isToLogin
    },



    mutations: {
        [types.SET_IS_AUTHENTICATED](state, isAuthenticated) {
            if (isAuthenticated) {
                state.isAuthenticated = isAuthenticated
            } else {
                state.isAuthenticated = false
            }
        },
        [types.SET_USER](state, user) {
            if (user) {
                state.user = user
            } else {
                state.user = {}
            }
        },
        // [types.SET_SHOW_AUTH_BOX](state, showAuthBox) {
        //     if (showAuthBox) {
        //         state.showAuthBox = showAuthBox
        //     } else {
        //         state.showAuthBox = false
        //     }
        // },
        [types.SET_IS_TOLOGIN](state, isToLogin) {

            state.isToLogin = isToLogin

        },
    },
    actions: {
        setIsAuthenticated: ({ commit }, data) => {
            commit(types.SET_IS_AUTHENTICATED, data)
        },
        setUser: ({ commit }, data) => {
            commit(types.SET_USER, data)
        },
        // setShowAuthBox: ({ commit }, data) => {
        //     commit(types.SET_SHOW_AUTH_BOX, data)
        // },
        setIsToLogin: ({ commit }, data) => {
            commit(types.SET_IS_TOLOGIN, data)
        },
        // 清空state
        clearCurrentState: ({ commit }) => {
            commit(types.SET_IS_AUTHENTICATED, false)
            commit(types.SET_USER, null)
            commit(types.SET_SHOW_AUTH_BOX, false)
            commit(types.SET_IS_TOLOGIN, null)

        }
    }
})

export default store;